public class Queue_By_Linked {
    class Node {
        int val;
        Node next;
        public Node(int val){
            this.val = val;
        }
    }
    public Node head;
    public Node tail;
//入队列
    public boolean offer(int val){
        Node cur = new Node(val);
    if(isEmpty()){
        this.head = cur;
        this.tail = this.head;
        return true;
    }else{
        this.tail.next = cur;
        this.tail = cur;
        return true;
    }
    }
    //出队列
    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
            int val = this.head.val;
            this.head = this.head.next;

        if(head==null){
            this.tail = null;
        }
        return val;
    }
    //获取队头元素
    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }else{
            return this.head.val;
        }

    }
    //获取队列中有效元素的个数
    public int size(){
        if(isEmpty()){
            return 0;
        }else{
            Node cur = this.head;
            int count = 0;
            while(cur!=null){
                cur = cur.next;
                count++;
            }
            return count;
        }
    }
    //检查队列是否为空
    public boolean isEmpty(){
        return this.head==null&&this.tail==null;
    }
}
